package cn.com.wxd.service.sms.webchinese;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.wxd.service.sms.SmsInfo;
import cn.com.wxd.service.sms.SmsSenter;
import cn.com.wxd.common.CharValidator;
import cn.com.wxd.common.thread.ThreadPoolUtil;

/**
 * Title:中国建网短信发送接口
 * Description:cn.com.wxd.service.sms.webchinese
 * <p>
 * Company: wxd网站系统
 * Copyright: Copyright (c) 2015
 * All right reserved.
 * Created time: 2016-4-13 下午3:10:38
 *
 * @author WangXuDong
 * @version 1.0
 */
public class SmsService_webchinese implements SmsSenter {
    private static final Logger log = LoggerFactory.getLogger(SmsService_webchinese.class);
    WebchineseSMS sms = new WebchineseSMS(SmsInfo.getUserName(), SmsInfo.getPassword());

    @Override
    public int sent(String phoneNum, String content) {
        if (content == null || content.trim().equals("")) {
            log.error("短信发送内容空");
            return -2;
        }
        if (phoneNum == null || !CharValidator.isMobile(phoneNum)) {
            return -1;
        }
        int resultInt = sms.sentMessage(phoneNum, content);
        if (resultInt == -1 || resultInt == -2) {
            log.error("短信发送用户不存在");
            return -1;
        } else if (resultInt == -3) {
            log.error("短信账户余额不足!");
            return -2;
        } else if (resultInt == -51 || resultInt == -14) {
            log.error("短信内容不合法，或短信签名不正确!格式为【短信签名】");
            return -3;
        } else {
            return 1;
        }
    }

    @Override
    public int sent(String[] phoneNums, String content) {
        if (content == null || content.trim().equals("")) {
            log.error("短信发送内容空");
            return -2;
        }
        if (phoneNums == null || phoneNums.length <= 0) {
            log.info("短信发送服务---请设置手机号！");
            return -1;
        }
        if (queryBalance() < 100) {
            log.error("短信账户余额不足!");
            return -3;
        }
        sms.sentMessages(phoneNums, content);
        return 1;
    }

    @Override
    public int queryBalance() {
        int resultInt = sms.queryBatch();
        return resultInt;
    }

    @Override
    public int asyncSent(final String phoneNum, final String content) {
        if (content == null || content.trim().equals("")) {
            log.error("短信发送内容空");
            return -2;
        }
        if (phoneNum == null || !CharValidator.isMobile(phoneNum)) {
            return -1;
        }
        try {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    sent(phoneNum, content);
                }
            };
            ThreadPoolUtil.submintThread(runnable);
            return 1;
        } catch (Exception e) {
            log.error("邮件发送失败!", e);
            return 0;
        }
    }

    @Override
    public int asyncSent(final String[] phoneNums, final String content) {
        if (content == null || content.trim().equals("")) {
            log.error("短信发送内容空");
            return -2;
        }
        if(phoneNums==null||phoneNums.length<=0){
            for (String phoneNum : phoneNums) {
                if (!CharValidator.isMobile(phoneNum)) {
                    log.info("发送短信---手机号码错误!");
                    return -1;
                }
            }
            log.info("短信发送服务---请设置手机号！");
            return -1;
        }
        try {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    sent(phoneNums, content);
                }
            };
            ThreadPoolUtil.submintThread(runnable);
            return 1;
        } catch (Exception e) {
            log.error("邮件发送失败!", e);
            return 0;
        }
    }
}
